<template>
	<view class="content">
		<exam-widght 
		        :dataList='QuestionList' 
		        :index="index" 
		        @select='selectItem' 
		        @selectFinish='selectFinish' 
		        @finish='finish'
		        :numBoxType='0'
		        :showIndexText='showIndexText'
		        :numBoxShow='numBoxShow'
		        >
		        </exam-widght>
				<view style="text-align: center;padding: 20rpx;">------------分割线-----------</view>
				<view>
					<!-- <button size="mini" @tap="jump()">跳转10题</button> -->
					 
					<button style="margin-left: 20rpx;" size="mini" @tap="switchIndexBox()">题号显示切换</button>
				</view>
				<page-head :title="title"></page-head>
			<!-- <view class="uni-padding-wrap uni-common-mt">
				<view class="uni-btn-v">
					<button type="default" @tap="modalTap">有标题的modal</button>
				
				</view>
			</view> -->
	</view>
</template>

<script>
	import examWidght from '@/components/Li-ExamWidght/Li-ExamWidght.vue';
	import uniPopup from "@/components/uni-popup/uni-popup.vue";
		import uniSection from '@/components/uni-section/uni-section.vue'
	var _this;
	export default {
		components: {
			examWidght,
			uniPopup,
			uniSection
		}, 
		data() {
			return {
				title: 'modal',
				modalHidden: true,
				modalHidden2: true,
				fldTestPaperID: undefined,
				fldTestRecordID: undefined,
				showIndexText:true, //题号文字是否显示
				numBoxShow:false, 
				items: [],
				index: 0,
				QuestionList: [],
			}
		},
		onLoad(opt) {
			_this = this;
			setTimeout(function(){
				_this.getTestPaper();
			},100)
		},
		onUnload() {},
		methods: {
			modalTap: function (str,mark) {
				
				console.log(str);
					console.log(mark);
				uni.showModal({
					title: "完成答题",
					content: `您的成绩是${mark}分
						您的错题是第${str}题
					`,
					showCancel: false,
					confirmText: "确定"
				})
			},

			jump(){
				this.index = 9;
			},
			//题号显示
			switchIndexBox(){
				this.numBoxShow = !this.numBoxShow;
			},
			//点击完成就执行
			finish(item) {
				//另外的功能写在组件里
				console.log("-----finish------")
				console.log(item)
				let arr = item.questions;
				let error = 0;
				let errArr = [];
				for(let k in arr){
					if(arr[k].fldAnswer != arr[k].rightAnswer){
						error++;
						errArr.push(arr[k].qid);
					}
				}
				let mark = parseFloat(((arr.length-error)/arr.length)*100);
				mark = mark.toFixed(2);
				let str = errArr.toString();
				// document.getElementById("pop").click();
				this.modalTap(str,mark);
			},
			//一选择就执行
			selectItem(item) {
				console.log("-----selectItem------")
				console.log(item)
			},
			//选择完成到下一题执行
			selectFinish(item) {
				console.log("-----selectFinish------")
				console.log(item)
				let index = item.newItem.index + 1;
				let title = index + '/' + item.newItem.total

				if (item.newItem.question.questionType == 0)
					title += "  单选题";
				else if (item.newItem.question.questionType == 1)
					title += "  多选题";
				else
					title += "  填空题";
				uni.setNavigationBarTitle({
					title: title
				});
			},
			getTestPaper() {
				// let data = require('../../static/data.json')  ;
				uni.request({
					url: 'http://wd1900144.pro.wdcase.com/carApi/test.php',
					method: 'GET',
					data: {
						act:'detail',
						fid:'1'
					},
					success: res => {
						let fld = res.data.fld;
						let question = res.data.question;
						
						for(let k in question){
							let	str = question[k].QuestionOptionList;
							
							let strs = new Array();
							//先将字符串分割成数组，再将数组转换成json格式
							
							
							strs=str.split(","); //字符分割 
							
							for(let i in strs){
								//数组转换成json格式
								//由于var 与 let作用域的问题导致我只能获取最后一个i 的值 let的作用域只在当前代码块
										let json = {};//重新定义
										//json 是中间储存变量 每次循环都重新定义是不影响结果的
										json.fldOptionText = strs[i];
										//此处i 不知为何变为字符 需要转换为数字
										let j =  parseInt(i)+1;
										json.fldOptionIndex = j;		
										strs[i] = json;							
							}
						// console.log(strs);
							question[k].QuestionOptionList = strs;
						}
						// console.log(question);
						fld.QuestionList = question;
					
							let data = fld;
						// console.log(data.QuestionList)
							_this.QuestionList = data.QuestionList;
							if (_this.QuestionList.length > 0) {
								let title = '1/' + _this.QuestionList.length
								if (_this.QuestionList[0].questionType == 0)
									title += "  单选题";
								else if (_this.QuestionList[0].questionType == 1)
									title += "  多选题";
								else
									title += "  填空题";
								uni.setNavigationBarTitle({
									title: title
								});
							}
					},
					fail: () => {},
					complete: () => {}
				});
				
				
			}
		}
	}
</script>

<style>
/* #id{
	position: absolute;
	left: 1000px;
}
 */



</style>
